﻿Public Class FormEmployeeEditor

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        If txtNameEm.Text.Trim() = "" Then
            MsgBox("Please Input Name")
            txtNameEm.Focus()
            Exit Sub
        End If
        If dtpDOB.Text.Trim() = "" Then
            MsgBox("Please Input brith of bay ")
            dtpDOB.Focus()
            Exit Sub
        End If
        If txtPOB.Text.Trim() = "" Then
            MsgBox("Please Input Place of Brith")
            txtPOB.Focus()
            Exit Sub
        End If
        If txtPhone.Text.Trim() = "" Then
            MsgBox("Please Input Number Phone")
            txtPhone.Focus()
            Exit Sub
        End If
        If txtAddress.Text.Trim() = "" Then
            MsgBox("Please Imput Address")
            txtAddress.Focus()
            Exit Sub
        End If
        If txtSalary.Text.Trim() = "" Then
            MsgBox("Please Input Salary")
            txtSalary.Focus()
            Exit Sub
        End If

        Cmd.Parameters("@Employee_Name") = txtNameEm.Text
        Cmd.Parameters("@Sex") = IIf(rdoM.Checked, "ប្រុស", "ស្រី")
        Cmd.Parameters("@Day_Of_Birth") = dtpDOB.Text
        Cmd.Parameters("@Place_Of_Birth") = txtPOB.Text
        Cmd.Parameters("@Phone") = txtPhone.Text
        Cmd.Parameters("@Address") = txtAddress.Text
        Cmd.Parameters("@Salary") = txtSalary.Text

        'Dim count As Int16 = Cmd.ExecuteScalar("SELECT COUT(*) FROM TBL_EMPLOYEE WHERE Employee_Name =@Employee_Name AND Day_Of_Birth =@Day_Of_Birth AND Employee_Id <>@Employee_Id")
        'If count > 0 Then
        '    MsgBox("Employee name is already exists!")
        '    txtNameEm.SelectAll()
        '    txtEmployee_Id.Focus()
        'End If


        If txtEmployee_Id.Text = "" Then
            Cmd.ExecuteNonQuery("INSERT INTO TBL_EMPLOYEE VALUES(@Employee_Name,@Sex,@Day_Of_Birth,@Place_Of_Birth,@Phone,@Address,@Salary,1)")
        Else
            Cmd.Parameters("@Employee_Id") = txtEmployee_Id.Text
            Cmd.ExecuteNonQuery("UPDATE TBL_EMPLOYEE SET Employee_Name=@Employee_Name,Sex=@Sex,Day_Of_Birth=@Day_Of_Birth,Place_Of_Birth=@Place_Of_Birth,Phone =@Phone,Address =@Address,Salary =@Salary,Active =1 WHERE Employee_Id =@Employee_Id")
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancle.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
End Class